/**
 * 登录
 */
$.ajaxSetup( {
	cache : false,
	async : false,
	type : 'POST',
	dataType : 'json',
	contentType : 'application/json'
});

$.extend( {
	login:function(){
		loadCookie();
		$("#btnLogin").click(function(){
			var account = $.trim($("#account").val());
			var pwd = $("#password").val();
			var verifyCode = $.trim($("#verifyCode").val());
			if(account == ""){
				ymPrompt.alert({title:"登录提示",message:"账号不能为空，请输入您的账号！",width:320,heigth:80,handler:function(tp){
					if(tp=='ok' || tp=='close'){
						$("#account").focus();
					}
				}});
				return false;
			}
			if(pwd == ""){
				ymPrompt.alert({title:"登录提示",message:"密码不能为空，请输入您的密码！",width:320,heigth:80,handler:function(tp){
					if(tp=='ok' || tp=='close'){
						$("#pwd").focus();
					}
				}});
				return false;
			}
			if(verifyCode == ""){
				ymPrompt.alert({title:"登录提示",message:"验证码不能为空，请输入右侧验证码！",width:320,heigth:80,handler:function(tp){
					if(tp=='ok' || tp=='close'){
						$("#verifyCode").focus();
					}
				}});
				return false;
			}
			var opts = "{\"account\":\""+account+"\",\"password\":\""+pwd+"\",\"verifyCode\":\""+verifyCode+"\"}";
			//alert(opts);
			$.post("login",opts,function(msg){
				if(msg == "verify"){
					ymPrompt.alert({title:"登录提示",message:"验证码不正确，请重新输入验证码！",width:320,heigth:80,handler:function(tp){
						if(tp=='ok' || tp=='close'){
							loadVerifyCode();
							$("#verifyCode").attr("value","");
							$("#verifyCode").focus();
						}
					}});
					return false;
				}else if(msg == "disable"){
					ymPrompt.alert({title:"登录提示",message:"此用户已被禁用，请联系管理员！",width:320,heigth:80,handler:function(tp){
						if(tp=='ok' || tp=='close'){
							loadVerifyCode();
							$("#account").attr("value","");
							$("#password").attr("value","");
							$("#verifyCode").attr("value","");
							$("#account").focus();
						}
					}});
					return false;
				}else if(msg == "faild"){
					ymPrompt.alert({title:"登录提示",message:"输入的账号或密码不正确，请重新输入！",width:320,heigth:80,handler:function(tp){
						if(tp=='ok' || tp=='close'){
							loadVerifyCode();
							$("#account").attr("value","");
							$("#password").attr("value","");
							$("#verifyCode").attr("value","");
							$("#account").focus();
						}
					}});
					return false;
				}else{
					setCookie(account);
					location.href = "main";
				}
			});
		});
		$("body").keydown(function(event){
			if(event.keyCode==13){
				$("#btnLogin").click();
				return false;
			}
		});
	},
	logout:function(){
		ymPrompt.confirmInfo({message:'是否确定注销登陆？',title:'注销登录',handler:function(tp){
			$.getJSON("logout?msg="+tp,function(msg){
				if(msg != "faild"){
					//removeCookie(msg);
					window.parent.location.href = "";
				}
			});
		}});
	}
});

var domain = "scpf";

var setCookie = function(user) {
	var now = new Date();
	now.setFullYear(now.getFullYear() + 1);
	var expires = "; expires=" + now.toGMTString();
	document.cookie = "LoginUser_" + domain + "=" + escape(user) + expires
	+ '; path=/;';
};

var loadCookie = function() {
	var cookies = document.cookie.split("LoginUser_" + domain + "=");
	if (cookies.length > 1) {
		values = cookies[1].split(";");
		$("#account").val(values[0]);
	}else{
		$("#account").val();
	}
	$.getJSON("isLogin",function(msg){
		if(msg){
			location.href = "main";
		}else{
			return false;
		}
	});
};

var removeCookie = function(user) {
	var now = new Date();
	now.setFullYear(now.getFullYear() - 1);
	var expires = "; expires=" + now.toGMTString();
	document.cookie = "LoginUser_" + domain + "=" + escape(user) + expires
	+ '; path=/;';
};

var loadVerifyCode = function() {
	var nowTime = new Date().getTime();
	$("#randImage").attr("src","verify?time=" + nowTime);
};
